package com.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;

import com.bean.ProductFeature;

public class Test2 {
	private SqlSessionFactory sqlSessionFactory=null;
	private SqlSession sqlSession= null;
	Logger logger= Logger.getLogger(Test2.class);
	private void creatSqlSessionFactory(){
		InputStream inputStream=null;
		try {
			inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
	}
	public void selectProductFeatureById(){
		try {
			sqlSession=sqlSessionFactory.openSession();
			ProductFeature productFeature=sqlSession.selectOne("productFeature.selectProductFeatureById", 1);
			logger.info(productFeature);
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();
		}
	}
	//findProductFeature
	private void findProductFeature(){
		try {
			sqlSession=sqlSessionFactory.openSession();
			List<ProductFeature>productFeatures= sqlSession.selectList("productFeature.findProductFeature","product_feature_desc like ('粉红')");
			logger.info(productFeatures);
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();
		}
	}
	private void insertProductFeature(){
		try {
			sqlSession=sqlSessionFactory.openSession();
			ProductFeature productFeature= new ProductFeature();
			productFeature.setProductFeatureTypeId(1);
			productFeature.setProductFeatureDesc("宝石绿");
			productFeature.setSort(1);
			sqlSession.insert("productFeature.insertProductFeature",productFeature);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();
		}
	}
	private void updateProductFeatureById(){
		try {
			sqlSession=sqlSessionFactory.openSession();
			ProductFeature productFeature= new ProductFeature();
			productFeature.setProductFeatureTypeId(1);
			productFeature.setProductFeatureDesc("宝石绿");
			productFeature.setSort(1);
			productFeature.setProductFeatureId(28);
			sqlSession.update("productFeature.updateProductFeatureById",productFeature);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();
		}
	}
	private void deleteProductFeatureById(){
		try {
			sqlSession=sqlSessionFactory.openSession();
			int i=sqlSession.delete("productFeature.deleteProductFeatureById",28);
			System.out.println(i);
			sqlSession.commit();
		} catch (Exception e) {
			sqlSession.rollback();
			logger.error(e.getLocalizedMessage());
		}finally{
			sqlSession.close();
		}
	}
	public static void main(String[] args) {
		Test2 test2= new Test2();
		test2.creatSqlSessionFactory();
		//test2.selectProductFeatureById();
		//test2.findProductFeature();
		//test2.insertProductFeature();
		//test2.updateProductFeatureById();
		test2.deleteProductFeatureById();
	}
}
